package psn.kiko.controller.blog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import psn.kiko.annotation.api.StableID;
import psn.kiko.constant.MessageConstant;
import psn.kiko.pojo.PageResult;
import psn.kiko.pojo.Result;
import psn.kiko.pojo.dto.BlogDTO;
import psn.kiko.pojo.vo.BlogVO;
import psn.kiko.service.business.system.BlogService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 博客控制器
 */
@RestController
@RequestMapping("/admin/blog")
@Api(tags = {"博客管理"})
public class BlogController{
	@Resource
	BlogService blogService;
	
	@GetMapping("/page/{pageNum}/{pageSize}")
	@ApiOperation("分页获取博客")
	@StableID(801)
	public Result getBlogs(
			@PathVariable("pageNum") @ApiParam("页码") int pageNum,
			@PathVariable("pageSize") @ApiParam("页码大小") int pageSize){
		PageResult<Object> pageResult = blogService.pagingGetBlogs(pageNum,pageSize);
		return Result.success(pageResult);
	}
	
	@PostMapping("/upload")
	@ApiOperation("上传博客")
	@StableID(802)
	public Result upload(@RequestBody @ApiParam("博客DTO") BlogDTO blogDTO){
		Integer r = blogService.addBlog(blogDTO);
		return Result.success(r);
	}
	
	@GetMapping("/{blogId}")
	@ApiOperation("根据id获取博客")
	@StableID(803)
	public Result getBlog(@PathVariable("blogId") @ApiParam("博客id") Integer blogId){
		BlogVO blogVO = blogService.getBlogById(blogId);
		return Result.success(blogVO);
	}
	
	@DeleteMapping("/batchDelete")
	@ApiOperation("批量删除博客")
	@StableID(804)
	public Result batchDelete(@RequestBody @ApiParam("博客id集合") List<Integer> ids){
		Integer r = blogService.deleteBlogs(ids);
		return Result.success(r);
	}
	
	@PutMapping("/update")
	@ApiOperation("根据id更新博客")
	@StableID(805)
	public Result updateBlog(@RequestBody @ApiParam("博客DTO") BlogDTO blogDTO){
		Integer r = blogService.updateBlog(blogDTO);
		return Result.success(r);
	}
	
	@PostMapping("/file/image")
	@ApiOperation("上传博客图片")
	@StableID(806)
	public Result uploadImage(
			@ApiParam(value = "需要上传的文件集合", allowMultiple = true) @RequestParam("file") MultipartFile[] files){
		List<String> list = blogService.batchAddFile(files);
		return Result.success(MessageConstant.FILE_UPLOAD_SUCCESS,list);
	}
}
